using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Return from subroutine
    /// </summary>
    public class Rtsd : Instruction
    {
        public override int CycleCount
        {
            get { return 2; }
        }

        public Rtsd()
            : base("rtsd")
        {
        }

        /// <summary>
        /// Return from subroutine will branch to the location specified by the 
        /// contents of rA plus the IMM field, sign-extended to 32 bits. This 
        /// instruction always has a delay slot. The instruction following the 
        /// RTSD is always executed before the branch target.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint sa = MicroblazeComponent.ReadRegister(SourceRegisterA);
            uint imm = GetImmediateValue(MicroblazeComponent);

            MicroblazeComponent.Pc =
              (uint)(sa + imm);

            MicroblazeComponent.DelayedBranchActive = true;
        }

        public override string ToString()
        {
            return string.Format("rtsd r{0}, {1}",
              SourceRegisterA,
              ImmediateValue
            );
        }
    }
}
